<template>
    <el-dialog v-model="dialogShow10" width="60%">
        <div class="titleShow">
          <div class="titles">{{ $t('licence.sys_upgrade') }}</div>
        </div>
        <el-table ref="singleTable" :data="editionList" highlight-current-row style="width: 100%">
          <el-table-column :label="$t('licence.sys_xuanze')" width="80">
            <template #default="scope">
              <el-radio :label="scope.$index" v-model="editionRadio"
                @change.native="getCurrentRow(scope.row)"></el-radio>
            </template>
          </el-table-column>
          <el-table-column property="versionNo" :label="$t('licence.versionNo')"></el-table-column>
          <el-table-column property="versionName" :label="$t('licence.versionName')"></el-table-column>
          <el-table-column property="upgradeTip" :label="$t('licence.upgradeTip')"
            :show-overflow-tooltip="true"></el-table-column>
          <el-table-column property="upgradeTime" width="200px" :label="$t('licence.upgradeTime')">
            <template #default="scope">
              {{ format.FormatDate(scope.row.upgradeTime) }}
            </template>
          </el-table-column>
          <el-table-column property="status" :label="$t('licence.sys_status')">
            <template #default="{ row }">
              <el-tag v-if="row.status == 0">{{ $t('licence.sys_normal') }}</el-tag>
              <el-tag type="danger" v-if="row.status == 1">{{
                $t('licence.sys_tingyong')
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column property="type" :label="$t('licence.sys_gujian')">
            <template #default="{ row }">
              <p v-if="row.type == 2">{{ $t('licence.sys_nibianqi') }}</p>
            </template>
          </el-table-column>
          <el-table-column property="remark" :label="$t('licence.remark')"></el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block" style="margin: 20px">
          <el-pagination background @size-change="editionSizeChange" @current-change="editionCurrentChange"
            :current-page="editionData.page" :page-size="editionData.pagesize" :page-sizes="[2, 4, 6, 8, 10]"
            layout="prev, pager, next,total, jumper" :total="editionData.total">
          </el-pagination>
        </div>
        <el-form ref="upgradeForm" :model="upgradeForm" :rules="rules" label-width="100px">
          <el-form-item :label="$t('licence.gengxinremark')" prop="remark">
            <el-input v-model="upgradeForm.remark" :placeholder="$t('licence.remark')" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogShow10 = false">{{ $t('licence.cancel') }}</el-button>
            <el-button type="primary" @click="instruction">{{ $t('licence.determine') }}</el-button>
          </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { commandControl } from "@/api/inveter/inveter";
import { getEditionLists } from "@/api/system/edition/edition"; //固件升级列表获取
import format from '@/utils/format'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const dialogShow10 = ref(false) //固件升级
const props = defineProps(['ids','lang','powerStation']);
const editionRadio = ref('')
// 固件升级分页
const editionData = reactive({
  page: 1, // 当前页码
  pagesize: 10,
  total: 0, // 总数
})
const upgradeForm = ref({
  remark: ''
})
const editionList = ref([]) //固件升级信息列表
const inverterId = ref('') //固件升级
const getCurrentRow = (row: any) => {
  inverterId.value = row.id;
}
const instruction = async ()=>{
  ElMessageBox.confirm(
      t("licence.SubmitInverter"),
      t("licence.Tips"),
      {
        confirmButtonText: t("licence.determine"),
        cancelButtonText: t("licence.cancel"),
        type: "warning",
      }
    )
      .then(async () => {
        let data = {
          commandId: 10,
          ids: props.ids.join(","), //逆变器id
          versionId: inverterId.value, //版本id
          remark: upgradeForm.value.remark,
          lang: props.lang,
          // stationId: powerStation.value.id,
        };
        await commandControl(data);
        ElMessage({
          type: "success",
          message: t("licence.success"),
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: t("licence.cancels"),
        });
      });
    dialogShow10.value = false;
}
// 固件升级获取列表
const getEditionList = async () => {
  let res = await getEditionLists({
    pageNo: editionData.page,
    pageSize: editionData.pagesize,
    type: 2,
  });
  editionData.total = res.data.total;
  editionList.value = res.data.list;
}
// 分页
const editionSizeChange = (val: any) => {
  editionData.pagesize = val;
  getfaultData();
}
const editionCurrentChange = (val: any) => {
  editionData.page = val;
  getfaultData();
}
const openDialog = async () => {
  dialogShow10.value = true;
  getEditionList();
}
defineExpose({
  openDialog
})
</script>